How to shrink a bootable disk image

Wed Nov 10 2021 00:00:00 GMT+0000 (Coordinated Universal Time)

This method worked for me. I had a 64GB disk image made from a botable USB stick, and I wanted to shrink it down so that it could fit on 16GB memory sticks.

https://superuser.com/questions/610819/how-to-resize-img-file-created-with-dd#comment2505407_1583784

Short summary in case of bit rot:

lsblk
sudo umount <the device from lsblk>
dd if=<the device from lsblk> of=/path/to/file/myimage.img
sudo modprobe loop
sudo losetup -f

That would give you the next free loop device number. If it were 25, then:

sudo losetup /dev/loop25 myimage.img
sudo partprobe /dev/loop25
sudo gparted /dev/loop25

Resize inside of Gparted

Then:

sudo losetup -d /dev/loop25
fdisk -l myimage.img

Check the end block and the block size from that command. If end block is 9181183 and block size is 512, then issue:

truncate --size=$[(9181183+1)*512] myimage.img